﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace UDS.Components
{
    /// <summary>
    /// Team 类
    /// </summary>
    public class Team
    {
        #region 获取组内成员

        /// <summary>
        /// 获取组内成员
        /// </summary>
        public SqlDataReader GetStaffInTeam(int TeamID)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TeamID", SqlDbType.Int, 20, TeamID)
            };

            try
            {
                // run the stored procedure
                return data.RunDataReader("dbo.sproc_GetStaffInTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获取组内成员出错!", ex);
            }
        }

        #endregion

        #region 获取非组内成员

        /// <summary>
        /// 获取组内成员
        /// </summary>
        public SqlDataReader GetStaffNotInTeam(int TeamID)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TeamID", SqlDbType.Int, 20, TeamID)
            };

            try
            {
                // run the stored procedure
                return data.RunDataReader("dbo.sproc_GetStaffNotInTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获取非组内成员出错!", ex);
            }
        }

        #endregion

        #region 获取组的订阅人

        /// <summary>
        /// 获取组的订阅人
        /// </summary>
        public SqlDataReader GetStaffSubscriptionTeam(int TeamID)
        {
            var data = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@TeamID", SqlDbType.Int, 20, TeamID)
            };

            try
            {
                // run the stored procedure
                return data.RunDataReader("dbo.sproc_GetStaffSubscriptionTeam", parameters);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("获取组的订阅人出错!", ex);
            }
        }

        #endregion

        #region 从组中删除某成员

        /// <summary>
        /// 从组中删除某成员
        /// </summary>
        /// <param name="staffids">人员id集合</param>
        /// <param name="TeamID">The id of the specified team.</param>
        /// <returns>是否成功</returns>
        public bool DeleteStaffFromTeam(string staffids, int TeamID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@StaffIDS", SqlDbType.NVarChar, 300, staffids),
                Database.MakeInParam("@TeamID", SqlDbType.Int, 8, TeamID)
            };
            try
            {
                return ((db.RunProc("dbo.sproc_DeleteStaffFromTeam", parameters) == 0) ? true : false);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("从组中删除某成员!", ex);
            }
        }

        #endregion

        #region 设置组长

        /// <summary>
        /// 设置组长
        /// </summary>
        /// <param name="staffids">人员id集合</param>
        /// <param name="TeamID">The id of the specified team.</param>
        /// <returns>是否成功</returns>
        public bool SetLeader(string staffids, int TeamID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@StaffIDS", SqlDbType.NVarChar, 300, staffids),
                Database.MakeInParam("@TeamID", SqlDbType.Int, 8, TeamID)
            };
            try
            {
                return ((db.RunProc("dbo.sproc_SetupLeader", parameters) == 0) ? true : false);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("设置组长出错!", ex);
            }
        }

        #endregion

        #region 加入成员至组

        /// <summary>
        /// 加入成员至组
        /// </summary>
        /// <param name="staffids">人员id集合</param>
        /// <param name="TeamID">The id of the specified team.</param>
        /// <returns>是否成功</returns>
        public bool AddMemberToTeam(string staffids, int TeamID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@StaffIDS", SqlDbType.NVarChar, 300, staffids),
                Database.MakeInParam("@TeamID", SqlDbType.Int, 8, TeamID)
            };
            try
            {
                return ((db.RunProc("dbo.sproc_AddStaffToTeam", parameters) == 0) ? true : false);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                throw new Exception("加入成员至组出错!", ex);
            }
        }

        #endregion
    }
}